<?php
include "../../../server/ToolServer.php";
include "../../../server/HredisServer.php";
// 用户判断
class index
{
    public $toolServer;
    public $redisServer;
    protected $now;
    protected $outTime = 20; //投广超时时间（秒）
    protected $oldUserTime = 60 * 60;  //老用户时间（秒）留存多久算老用户
    protected $currentVersion = 2;  //限制版本，低于该版本不用验证，直接通过

    protected $clientCacheTime = 60*60;  //客户端缓存屏蔽配置的时间（秒）

    protected $checkList;
    protected $params = []; // request传参

    protected  $resultCode = 1;

    protected $dumpPath = '/pages/Task';

    protected $blackCitys = '佛山,深圳,东莞,广州,西安,上海,福州,厦门,龙岩,南京,镇江,常州,杭州,成都,北京,香港,澳门,台湾';

    public function __construct()
    {
        $this->now = time();
        $params = $_REQUEST;
        if (empty($params['app_version'])) {
            $params = json_decode(file_get_contents("php://input"), true);
        }
        $this->params = $params;
        $this->checkList = [
            'ip' => 1,
            'oaid' => 1,
            'city' => 1,
            'time' => 1, //华为投放没有时间戳
            'isOld' => 1,
            'version' => 1,
        ];
        $this->toolServer = new ToolServer();
        $this->redisServer = new HredisServer();
    }

    public function getIp()
    {
        $ipArr = $this->toolServer->getClientIp();
        $ip = empty($ipArr['http_x_forwarded_for']) ? $ipArr['remote_addr'] : $ipArr['http_x_forwarded_for'];
        return $ip;
    }

    public function pass($data){
        $data['cacheTime'] = $this->clientCacheTime;
        $data['path'] = $this->dumpPath;
        $data['resCode'] = $this->resultCode;
        $this->toolServer->success('success', $data);
    }

    public function exc()
    {
        $params = $this->params;
        try {
            file_put_contents('./log/' . date('Ymd') . '.log', 'getSetting:'.json_encode($params) . "\r\n", FILE_APPEND);
        } catch (Exception $e) {
        }
        $noHapPass = 3;
        $msg = 'success';
        // 非hap验证
        if (empty($params['app_version'])) {
            $noHapPass = 0;
            $msg = 'invalid version';
            $this->resultCode = 10001;
        } else {
            if ($params['app_version'] < $this->currentVersion) {
                $noHapPass = 1;
                $msg = 'old version';
                $this->resultCode = 10002;
            }
        }
        if ($noHapPass == 3) {
            $uTime = $params['utime'];
            if ($this->now - $uTime > $this->oldUserTime) {
                $noHapPass = 1;
                $msg = 'old user';
                $this->resultCode = 10003;
            }
        }

        if (in_array($noHapPass, [0, 1])) {
            //非hap验证
            $this->pass(['pass' => $noHapPass, 'msg' => $msg]);
        } else {
            if(empty($params['hap'])){
                // 没有hap链接
                $this->pass(['pass' => 10011, 'msg' => 'noHap']);
            }else{
                //hap验证
                $pass = 1;
                $checkList = $this->checkList;
                $hap = json_decode($params['hap'],true);
                // 时间戳验证
                if ($pass == 1 && $checkList['time'] == 1) {
                    if (!empty($hap['stamp'])) {
                        if ($hap['stamp'] - $params['ts'] > $this->outTime) {
                            $pass = 0;
                            $msg = 'invalid stamp';
                            $this->resultCode = 10004;
                        }
                    }
                }
                // ip验证
                if ($pass == 1 && $checkList['ip'] == 1) {
                    if (empty($hap['ip'])) {
                        $pass = 0;
                        $msg = 'invalid ip1';
                        $this->resultCode = 10005;
                    } else {
                        $clientIp = $this->getIp();
                        $this->params['ip'] = $params['ip'] = $clientIp;
                        $clientIpArr = explode('.', $clientIp);
                        $clientIpString = ($clientIpArr[0] ?? '') . ($clientIpArr[1] ?? '') . ($clientIpArr[2] ?? '');
                        $hapArr = explode('.', $hap['ip']);
                        $hapString = ($hapArr[0] ?? '') . ($hapArr[1] ?? '') . ($hapArr[2] ?? '');
                        if ($clientIpString !== $hapString) {
                            $pass = 0;
                            $msg = 'invalid ip2';
                            $this->resultCode = 10006;
                        }
                    }
                }
                // oaid验证
                if ($pass == 1 && $checkList['oaid'] == 1) {
                    if (empty($hap['oaid'])) {
                        $pass = 0;
                        $msg = 'invalid oaid';
                        $this->resultCode = 10007;
                    } else {
                        $clientOaid = $params['oaid'];
                        $hapOaid = $hap['oaid'];
                        if ($clientOaid !== $hapOaid) {
                            $pass = 0;
                            $msg = 'invalid oaid2';
                            $this->resultCode = 10008;
                        }
                    }
                }
                // 屏蔽地区验证
                if ($pass == 1 && $checkList['city'] == 1) {
                    $cityArr = explode(',', $this->blackCitys);
                    $ipArr = explode('.', $params['ip']);
                    $clientIp = $ipArr[0] . '.' . $ipArr[1] . '.' . $ipArr[2];
                    $redisAddress = $this->redisServer->hget('ipAddress', $clientIp);
                    if (!empty($redisAddress)) {
                        $this->params['address'] = $redisAddress;
                        $areaArr = explode('|', $redisAddress);
                        $clientCity = $areaArr[3];
                        if (in_array($clientCity, $cityArr)) {
                            $pass = 0;
                            $msg = '该地区不在范围内1';
                            $this->resultCode = 10009;
                        }
                    }else{
                        include_once "../../../server/XkRegion.php";
                        $regionServer = new  XkRegion();
                        $redisAddress = $regionServer->memorySearch($params['ip']);
                        $areaArr = explode('|', $redisAddress['region']);
                        if (isset($areaArr[3])) {
                            $this->params['address'] = $redisAddress;
                            $clientCity = $areaArr[3];
                            if (in_array($clientCity, $cityArr)) {
                                $pass = 0;
                                $msg = '该地区不在范围内2';
                                $this->resultCode = 10010;
                            }
                        }
                    }
                }
                $key = 'xk:CheckList';
                $redisServer = $this->redisServer;
                $params['res'] = ['pass'=>$pass,'check_msg'=>$msg,'resCode'=>$this->resultCode];
                $redisServer->rpush($key, json_encode($params));
            }


            $this->pass(['pass' => $pass, 'msg' => $msg]);
        }

    }
}

$wap = new index();
$wap->exc();
?>
